package com.china08.yxyapi.api.comm.weike;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyKnowledge;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyKnowledgeRepository;

/**
 * 微课公共接口---根据知识点id级联查询一级,二级知识点
 * 
 */
@CrossOrigin
@RestController
public class WeikeKnowLevelController extends YxyApiBaseController {

    @Autowired
    private YxyKnowledgeRepository knowRepository;

    @RequestMapping(value = "/api_yw/comm/weike/know/level", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        reqModel.check();
        List<RespModel> list = new ArrayList<RespModel>();
        List<YxyKnowledge> knowledges = knowRepository.findByStageAndSubjectAndDeleteFlagAndParentId(reqModel.stage,
                reqModel.subject, "1", reqModel.pid);
        for (YxyKnowledge yxyKnowledge : knowledges) {
            RespModel respModel = new RespModel();
            respModel.id = yxyKnowledge.getId();
            respModel.pId = yxyKnowledge.getParentId();
            respModel.name = yxyKnowledge.getName();
            list.add(respModel);
        }
        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String stage;// 学段

        public String subject;// 科目

        public Integer pid;// 当前知识点id

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(stage, null), "学段不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(subject, null), "科目不能为空");
            Assert.notNull(pid, "知识点父id不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

        public Integer id;// 知识点id

        public Integer pId;// 知识点的父id

        public String name;// 知识点名称
    }

}
